//
//  DYZMySourceController.m
//  DYZCars
//
//  Created by MF on 16/8/19.
//  Copyright © 2016年 MF. All rights reserved.
//

#import "DYZMySourceController.h"
#import "DYZMySourceCell.h"
#import "DYZMySourceInfo.h"
#import "DYZMySourceDetailController.h"

static NSString *mysourceCell = @"mysourceCell";
@interface DYZMySourceController ()

@property (nonatomic, strong)NSArray *mySourceData;
@end

@implementation DYZMySourceController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    [self.tableView registerNib:[UINib nibWithNibName:@"DYZMySourceCell" bundle:nil] forCellReuseIdentifier:mysourceCell];
    self.tableView.estimatedRowHeight = 100;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//#warning Incomplete implementation, return the number of sections
//    return ;
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.mySourceData.count;;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DYZMySourceCell *cell = [tableView dequeueReusableCellWithIdentifier:mysourceCell forIndexPath:indexPath];
    cell.sourceInfo = self.mySourceData[indexPath.row];

    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"编辑我的车源");
    
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"mySourceDetail" bundle:nil];
    DYZMySourceDetailController *vc = [sb instantiateInitialViewController];
    vc.sourceInfo = self.mySourceData[indexPath.row];
    [self.navigationController pushViewController:vc animated:true];
}
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}



// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}

//加载数据

- (void)loadData {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"mySource.plist" ofType:nil];
    NSArray *tempAarray = [NSArray arrayWithContentsOfFile:path];
    NSArray *array = [NSArray yy_modelArrayWithClass:[DYZMySourceInfo class] json:tempAarray];
    self.mySourceData = array;
    
    [self.tableView reloadData];
    NSLog(@"%@",self.mySourceData);
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
